﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;

namespace SIPO.Web.Forms.Administration.Catalog
{
    public partial class recSubSubSectorform : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recSubSector> sectors = recSubSectorDal.GetListWithSectorCode();

                ddlSubSector.Items.Clear();
                sectors.ForEach(delegate(recSubSector sector)
                {
                    ddlSubSector.Items.Add(new ListItem( sector.recSector.SectorCode +"."+sector.SubSectorCode + " - " + sector.SubSectorName, sector.SubSectorID.ToString()));
                });

                ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sektor --", string.Empty));


                recRegistrationLabelRepo recRegistrationLabelDal = new recRegistrationLabelRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recRegistrationLabel> registrations = recRegistrationLabelDal.GetList();

                ddlRegistrationLabel.Items.Clear();
                registrations.ForEach(delegate(recRegistrationLabel registration)
                {
                    ddlRegistrationLabel.Items.Add(new ListItem(registration.RegistrationLabelName , registration.RegistrationLabelID.ToString()));
                });

                ddlRegistrationLabel.Items.Insert(0, new ListItem("-- pilih registrasi --", string.Empty));  

                if (id > 0) // Edit an existing item
                {
                    recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recSubSubSector recSubSubSector = recSubSubSectorDal.GetById(id);
                    if (recSubSubSector != null)
                    {
                        hfID.Value = id.ToString();
                        txtCode.Text = recSubSubSector.SubSubSectorCode;
                        txtName.Text = recSubSubSector.SubSubSectorName;
                        ddlSubSector.Items.FindByValue(recSubSubSector.SubSectorID.ToString()).Selected = true;
                        chkIsEmiten.Checked = recSubSubSector.IsEmiten ?  recSubSubSector.IsEmiten : false;
                        chkIsIndividual.Checked = recSubSubSector.IsIndividual ? recSubSubSector.IsIndividual : false;
                        ddlRegistrationLabel.Items.FindByValue(recSubSubSector.RegistrationLabelID.ToString()).Selected = true;
                        ddlSubSector.Enabled = false;
                        txtCode.ReadOnly = true;
                    }
                }
                GetPrivilege();
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }
}
